<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $id
 * @property string $username
 * @property string $first_name
 * @property string $last_name
 * @property string $password
 * @property string $email
 * @property integer $access_role
 * @property integer $group_id
 *
 * The followings are the available model relations:
 * @property Groups $group
 */
class User extends CActiveRecord {
    // Predefined Access Roles

    const ROLE_REGISTERED = 1, ROLE_ADMIN = 9, ROLE_DEVELOPER = 99;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'users';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
            array('username, first_name, last_name, password, email, access_role, group_id', 'required'),
            array('username, password', 'length', 'min' => 6, 'max' => 32),
            array('first_name, last_name', 'length', 'max' => 50),
            array('email', 'length', 'max' => 128),
            array('email', 'email'),
            array('first_name, last_name, email', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        return array(
            'group' => array(self::BELONGS_TO, 'Group', 'group_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'username' => 'Username',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'password' => 'Password',
            'email' => 'Email',
            'access_role' => 'Access Role',
            'group_id' => 'Group',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {

        $criteria = new CDbCriteria;

        $criteria->compare('username', $this->username, true);
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('access_role', $this->access_role);
        $criteria->compare('group_id', $this->group_id);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    /**
     * beforeSave
     */
    protected function beforeSave() {
        if ($this->isNewRecord) {
            $this->password = $this->hashPassword($this->password);
        }

        return parent::beforeSave();
    }

    /**
     * Generates the password hash.
     * @param string password
     * @return string hash
     */
    public function hashPassword($password) {
        $passwordHash = new PasswordHash(Yii::app()->params['phpass']['iteration_count_log2'], Yii::app()->params['phpass']['portable_hashes']);
        return $passwordHash->HashPassword($password);
    }

    /**
     * Checks if the given password is correct.
     * @param string the password to be validated
     * @return boolean whether the password is valid
     */
    public function checkPassword($password) {
        $passwordHash = new PasswordHash(Yii::app()->params['phpass']['iteration_count_log2'], Yii::app()->params['phpass']['portable_hashes']);
        return $passwordHash->checkPassword($password, $this->password);
    }

    /**
     * Define the label for each access roles.
     * @param type $role
     * @return string 
     */
    static function getAccessRoleList($role = null) {
        $accessRoleList = array();

        if (Yii::app()->user->isDeveloper)
            $accessRoleList = array(
                self::ROLE_REGISTERED => 'Registered',
                self::ROLE_ADMIN => 'Administrator',
                self::ROLE_DEVELOPER => 'Developer'
            );
        else
            $accessRoleList = array(
                self::ROLE_REGISTERED => 'Registered',
                self::ROLE_ADMIN => 'Administrator'
            );

        if ($role)
            return $accessRoleList[$role];
        else
            return $accessRoleList;
    }

}